package com.awaken.content.service.impl;

import com.awaken.content.mapper.ProductReviewMapper;
import com.awaken.content.models.dto.ProductReviewDTO;
import com.awaken.content.models.entity.ProductReview;
import com.awaken.content.models.request.ProductReviewRequest;
import com.awaken.content.service.ProductReviewService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductReviewServiceImpl extends ServiceImpl<ProductReviewMapper, ProductReview> implements ProductReviewService {
    
    @Override
    public ProductReviewDTO createReview(ProductReviewRequest request) {
        ProductReview review = new ProductReview();
        BeanUtils.copyProperties(request, review);
        review.setStatus(1);
        review.setCreateTime(LocalDateTime.now());
        review.setUpdateTime(LocalDateTime.now());
        
        save(review);
        
        ProductReviewDTO dto = new ProductReviewDTO();
        BeanUtils.copyProperties(review, dto);
        return dto;
    }
    
    @Override
    public ProductReviewDTO getReviewById(Long id) {
        ProductReview review = getById(id);
        if (review == null) {
            return null;
        }
        
        ProductReviewDTO dto = new ProductReviewDTO();
        BeanUtils.copyProperties(review, dto);
        return dto;
    }
    
    @Override
    public List<ProductReviewDTO> getReviewsByProductId(Long productId) {
        QueryWrapper<ProductReview> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId)
                .eq("status", 1)
                .orderByDesc("create_time");
        List<ProductReview> reviews = list(queryWrapper);
        
        return reviews.stream().map(review -> {
            ProductReviewDTO dto = new ProductReviewDTO();
            BeanUtils.copyProperties(review, dto);
            return dto;
        }).collect(Collectors.toList());
    }
    
    @Override
    public List<ProductReviewDTO> getReviewsByUserId(Long userId) {
        QueryWrapper<ProductReview> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("status", 1)
                .orderByDesc("create_time");
        List<ProductReview> reviews = list(queryWrapper);
        
        return reviews.stream().map(review -> {
            ProductReviewDTO dto = new ProductReviewDTO();
            BeanUtils.copyProperties(review, dto);
            return dto;
        }).collect(Collectors.toList());
    }
    
    @Override
    public ProductReviewDTO updateReview(Long id, ProductReviewRequest request) {
        ProductReview review = getById(id);
        if (review == null) {
            return null;
        }
        
        BeanUtils.copyProperties(request, review);
        review.setUpdateTime(LocalDateTime.now());
        updateById(review);
        
        ProductReviewDTO dto = new ProductReviewDTO();
        BeanUtils.copyProperties(review, dto);
        return dto;
    }
    
    @Override
    public void deleteReview(Long id) {
        ProductReview review = getById(id);
        if (review != null) {
            review.setStatus(0);
            review.setUpdateTime(LocalDateTime.now());
            updateById(review);
        }
    }
} 